#!/usr/bin/env bash

# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cd build || exit 1
make DESTDIR=${install_root} install || exit 1

triple=${arch}-unknown-linux-gnu

mkdir -p ${install_root}/usr/lib/${triple} || exit 1
for i in $(ls ${install_root}/usr/lib/); do
    if [ "${i}" != "${triple}" ]; then
        ln -sfv ../$i ${install_root}/usr/lib/${triple}/ || exit 1
    fi
done

sed -i 's/\/usr\/lib\///g' ${install_root}/usr/lib/libc.so || exit 1
sed -i 's/\/usr\/lib\///g' ${install_root}/usr/lib/libm.so || exit 1
sed -i 's/\/usr\/lib\///g' ${install_root}/usr/lib/libm.a || exit 1

if [ "${arch_bits}" = "64" ]; then
    mkdir -p ${install_root}/lib64 || exit 1
    for i in ${install_root}/usr/lib/ld-*.so*; do
        ldso=${i##*\/}
        ln -sfv /usr/lib/${ldso} $install_root/lib64/${ldso} || exit 1
    done
fi
